package querylog.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @typeName EncodingFilter
 * @Description: 全局编码过滤
 * @author wangxy
 * @date 2017年11月30日下午2:34:18
 * @version v1.0
 */
public class EncodingFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/**
	 * @Title doFilter
	 * @Description 编码过滤并放行
	 * @param @param request
	 * @param @param response
	 * @param @param chain
	 * @param @throws IOException
	 * @param @throws ServletException
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO 处理请求乱码
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletRequest myRequest = new MyRequest(httpServletRequest);
		//处理响应乱码
		response.setContentType("text/html;charset=utf-8");
		chain.doFilter(myRequest, response);//放行

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	class MyRequest extends HttpServletRequestWrapper {
		private HttpServletRequest request;
		private boolean hasEncode;

		public MyRequest(HttpServletRequest request) {
			super(request);
			this.request = request;
		}

		/**
		 * @Title getParameter
		 * @Description 其中调用getParameterMap进行编码处理
		 * @param @param name
		 * @param @return
		 */
		@Override
		public String getParameter(String name) {
			// TODO Auto-generated method stub
			Map<String, String[]> parameterMap = getParameterMap();
			String[] values = parameterMap.get(name);
			if (values == null) {
				return null;
			}
			return values[0];
		}

		/**
		 * @Title getParameterMap
		 * @Description 编码处理
		 * @param @return
		 */
		@Override
		public Map getParameterMap() {
			// TODO Auto-generated method stub
			String method = request.getMethod();
			//针对post方式进行请求编码处理
			if (method.equalsIgnoreCase("post")) {
				try {
					request.setCharacterEncoding("utf-8");
					return request.getParameterMap();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//针对get方式进行请求编码处理
			} else if (method.equalsIgnoreCase("get")) {
			
				Map<String, String[]> parameterMap = request.getParameterMap();
				if (!hasEncode) {
					for (String parameterName : parameterMap.keySet()) {
						String[] values = parameterMap.get(parameterName);
						if (values != null) {
							for (int i = 0; i < values.length; i++) {
								try {
									values[i] = new String(
											values[i].getBytes("ISO-8859-1"),
											"utf-8");
								} catch (UnsupportedEncodingException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}

					}
					hasEncode = true;
				}
				return parameterMap;
			}

			return super.getParameterMap();
		}

		/**
		 * @Title getParameterValues
		 * @Description 其中调用getParameterMap进行编码处理
		 * @param @param name
		 * @param @return
		 */
		@Override
		public String[] getParameterValues(String name) {
			// TODO Auto-generated method stub
			Map<String, String[]> parameterMap = getParameterMap();
			String[] values = parameterMap.get(name);
			return values;
		}

	}

}
